console.log("hello");
var port = null;
var rules = []
var intervalVar = null;
/*
rule1 = {rule_type: "at",
        time1: new Date(2001, 0, 1, 0, 28),
        urls: ["http://www.google.com"]}
rule2 = {rule_type: "always",
        interval: 2,
        urls: ["http://www.google1.com", "http://www.google2.com", "http://www.google3.com"]}
rule3 = {rule_type: "between",
        time1: new Date(2001, 0, 1, 1, 30),
        time2: new Date(2001, 0, 1, 1, 52),
        urls: ["http://www.t1.com", "http://www.t2.com"]}

*/
last_url = ""
//rules = [rule1,rule2,rule3]
rule1 = {rule_type: "always",
        time1: "",
        urls: ["http://www.google.com"]};

rules = [rule1];
function evalRule(rule) {
  now = new Date();
  switch(rule.rule_type) {
    case "at":
      time1 = new Date(rule.time1);
      if (now.getHours() == time1.getHours() &&
          now.getMinutes() == time1.getMinutes()) {
        return true;
      }
      break;
    case "between":
      time1 = new Date("2001/01/01 " + rule.time1);
      time2 = new Date("2001/01/01 " + rule.time2);
      if (now.getHours() >= time1.getHours() &&
          now.getHours() <= time2.getHours()) {
        if (now.getMinutes() >= time1.getMinutes() &&
            now.getMinutes() <= time2.getMinutes()) {
          return true;
        }
      }
      break;
    case "always":
      return true;
  }
  return false;
}

function getUrl(rule) {
  if (rule.urls.length <= 0) {
    return null;
  }
  if (!rule.hasOwnProperty("url_index")) {
    rule.url_index = 0;
  }
  if (!rule.hasOwnProperty("seconds_elapsed")) {
    rule.seconds_elapsed = 0;
  }
  if (!rule.hasOwnProperty("interval")) {
    rule.interval = 1;
  }
  rule.seconds_elapsed++;
  if (rule.seconds_elapsed >= rule.interval) {
    rule.seconds_elapsed = 0;
    cycleUrl(rule)
  }
  return rule.urls[rule.url_index];
}

function cycleUrl(rule) {
  rule.url_index++;
  if (rule.url_index >= rule.urls.length) {
    rule.url_index = 0;
  }
}

function setUrl(rule) {
  url = getUrl(rule);
  if (url != last_url) {
    
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      console.log(url);
      console.log(tabs);
      chrome.tabs.update(tabs[0].id, {url: url})
    });
    last_url = url;
  }
}

function evalRules(rules) {
  console.log("running");
  success_rule_num = -1;
  for (i=0; i < rules.length; i++) {
    if (evalRule(rules[i])) {
      success_rule_num = i;
    }
  }
  if (success_rule_num >= 0) {
    setUrl(rules[success_rule_num]);
  }
}

chrome.runtime.onConnect.addListener(function(popupPort) {
  console.assert(popupPort.name = "timed_url");
  port = popupPort;
  popupPort.onMessage.addListener(function(msg) {
    if (msg.cmd == "start") {
      console.log(msg.rules)
      rules = msg.rules
      clearInterval(intervalVar);
      intervalVar = setInterval(function() {
        evalRules(rules);
      }, 1000);
      chrome.browserAction.setIcon({'path': 'icon.png'});
    }
    if (msg.cmd == "stop") {
      clearInterval(intervalVar);
      chrome.browserAction.setIcon({'path': 'icon-disabled.png'});
    }
    
    if (msg.cmd == "get_rules") {
      port.postMessage(rules);
    }
  });
});
